package com.cppsu.cgx.libraryseatselection.services.impl;

import com.cppsu.cgx.libraryseatselection.dto.TipOffsParam;
import com.cppsu.cgx.libraryseatselection.entity.Seat;
import com.cppsu.cgx.libraryseatselection.entity.TipOffs;
import com.cppsu.cgx.libraryseatselection.entity.User;
import com.cppsu.cgx.libraryseatselection.exception.ApiException;
import com.cppsu.cgx.libraryseatselection.mapper.*;
import com.cppsu.cgx.libraryseatselection.services.SeatService;
import com.cppsu.cgx.libraryseatselection.services.TipOffsService;
import com.cppsu.cgx.libraryseatselection.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.mybatis.dynamic.sql.SqlBuilder.*;

/**
 * @Author: guibo
 * @Description:
 * @Date: Created in 16:32 2021/3/15
 * @Modified By:
 */
@Service
public class TipOffsServiceImpl implements TipOffsService {
    @Autowired
    TipOffsMapper tipOffsMapper;
    @Autowired
    UserService userService;
    @Autowired
    SeatMapper seatMapper;
    @Override
    public int add(TipOffsParam tipOffsParam) {
        Pattern p = Pattern.compile("[^0-9]");
        Matcher m = p.matcher(tipOffsParam.getPosition());
        int floorNumber = Integer.parseInt(m.replaceAll("").trim());
        List<Seat> seats = seatMapper.select(c->c
                .leftJoin(FloorDynamicSqlSupport.floor)
                .on(SeatDynamicSqlSupport.floorId,equalTo(FloorDynamicSqlSupport.id))
                .where(FloorDynamicSqlSupport.floorNumber,isEqualTo(floorNumber))
                .and(SeatDynamicSqlSupport.number,isEqualTo(tipOffsParam.getNumber()))
        );
        if(!seats.isEmpty()){
            TipOffs tipOffs = new TipOffs();
            tipOffs.setPosition(tipOffsParam.getPosition());
            tipOffs.setSeatNumber(tipOffsParam.getNumber());
            tipOffs.setCreateTime(new Date());
            tipOffs.setHandleStatus(0);
            tipOffs.setNote(tipOffsParam.getNote());
            tipOffs.setUserId(userService.getCurrentUser().getId());
            tipOffs.setSeatId(seats.get(0).getId());
            return tipOffsMapper.insert(tipOffs);
        }
        throw new ApiException("座位信息填写有误，举报失败");
    }

    @Override
    public int update(TipOffs tipOffs) {
        return 0;
    }

    @Override
    public int delete(List<Long> ids) {
        return tipOffsMapper.delete(c->c.where(TipOffsDynamicSqlSupport.id,isIn(ids)));
    }

    @Override
    public List<TipOffs> list(TipOffs tipOffs) {
        return null;
    }

    @Override
    public List<TipOffs> listByCurrentUser() {
        User user = userService.getCurrentUser();
        return tipOffsMapper.select(c->c
                .where(TipOffsDynamicSqlSupport.userId,isEqualTo(user.getId()))
                .orderBy(TipOffsDynamicSqlSupport.createTime.descending())
        );
    }
}
